package com.ateam.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ateam.biz.SharesBiz;
import com.ateam.pojo.Companies;
import com.ateam.pojo.Shares;
import com.ateam.pojo.Users;
import com.ateam.util.DateFactory;
import com.ateam.util.PojoFactory;

/**
 * @author WuYuan
 * 
 */
@Controller
public class SharesController {
	@Resource(name = "sharesBiz")
	private SharesBiz sharesBiz;

	// 公司层面用户发表说说
	@ResponseBody
	@RequestMapping("usershare.do")
	public boolean usershare(Shares share, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				share.setContent("于" + DateFactory.getDateToStr(new Date()) + "发布说说: " + share.getContent());
				if (1 == sharesBiz.addShareByUser(share, (Users) session.getAttribute("currentUser"), 1, 1)) {
					System.out.println("usersharecontroller添加share成功");
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 公司层面获取所有说说
	@ResponseBody
	@RequestMapping("getAllShares.do")
	public List<Shares> getAllShares(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Companies currentCompany = (Companies) session.getAttribute("currentCompany");
				return sharesBiz.getShares(currentCompany.getCompanyid());
			} else
				return null;
		} else
			return null;
	}

	// 用户层面发表说说
	@ResponseBody
	@RequestMapping("projectusershare.do")
	public boolean projectusershare(String projectsharecontent, String projectid, HttpSession session,
			HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Shares share = PojoFactory.getShare();
				share.setContent("于" + DateFactory.getDateToStr(new Date()) + "发表项目说说: " + projectsharecontent);
				if (1 == sharesBiz.addShareByUser(share, (Users) session.getAttribute("currentUser"), 1,
						Integer.parseInt(projectid))) {
					System.out.println("usersharecontroller添加share成功");
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 获取项目的所有说说
	@ResponseBody
	@RequestMapping("projectgetAllShares.do")
	public List<Shares> projectgetAllShares(String projectid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				List<Shares> shares = sharesBiz.getSharesByProjectid(Integer.parseInt(projectid));
				return shares;
			} else
				return null;
		} else
			return null;
	}

	// 提交小组说说
	@ResponseBody
	@RequestMapping("groupusershare.do")
	public boolean groupusershare(String groupsharecontent, String groupid, HttpSession session,
			HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Shares share = PojoFactory.getShare();
				share.setContent("于" + DateFactory.getDateToStr(new Date()) + "发表小组说说: " + groupsharecontent);
				if (1 == sharesBiz.addShareByUser(share, (Users) session.getAttribute("currentUser"),
						Integer.parseInt(groupid), 1)) {
					System.out.println("usersharecontroller添加share成功");
					return true;
				} else
					return false;
			} else
				return false;
		} else
			return false;
	}

	// 获取小组所有说说
	@ResponseBody
	@RequestMapping("groupgetAllShares.do")
	public List<Shares> groupgetAllShares(String groupid, HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				List<Shares> shares = sharesBiz.getSharesByGroupid(Integer.parseInt(groupid));
				return shares;
			} else
				return null;
		} else
			return null;
	}

	// 公司层面，查看自己的所有动态
	@ResponseBody
	@RequestMapping("usergetAllShares.do")
	public List<Shares> usergetAllShares(HttpSession session, HttpServletRequest request) {
		if (session.getAttribute("currentUser") != null) {
			if (session.getAttribute("currentCompany") != null) {
				Users user = (Users) session.getAttribute("currentUser");
				List<Shares> shares = sharesBiz.getSharesByUserid(user.getUserid());
				return shares;
			} else
				return null;
		} else
			return null;
	}
}
